/**
 * Library for context-sensitive WebHelp
 * 
 * contains:
 * 	Namespace ecw
 *  ecw.configuration, could be overwritten for customization
 *  ecw.HelpSystem Mainclass
 *  
 * @author embrace.toolkit
 * @version $Id$
 */

/**
 * Defining the namespace.
 */
var ecw = {};

/**
 * Contains the configuration settings for ECW. Could be
 * overwritten for customization. Following parameters are
 * used:
 * 
 * <ul>
 * 	<li>element - ID of the main HelpContainer</li>
 *  <li>handle - Click Area for displaying the help box.</li>
 *  <li>content - ID of the help content box.</li>
 *  <li>highlighter -  CSS-Classname for highlighting help items</li>
 *  <li>servicePath - Path to the help files</li>
 *  <li>suffix - the file suffix for your help files</li>
 * </ul>
 */
ecw.configuration = {
	element: "ecwContainer",
	handle: "ecwHandle",
	content: "ecwContent",
	highlighter: "ecwHighlighted",
	servicePath: "/demo/help/",
	suffix: ".html"
};

/**
 * HelpSystem main class
 * 
 * provides the ECW HelpSystem for web applications.
 * @package ecw
 */
ecw.HelpSystem = Class.create({
	initialize: function() {
		this.toggle();
		$(ecw.configuration.element).select("#" + ecw.configuration.handle).each(function(handle) {
			handle.observe("click", this.toggle.bind(this));
		}.bind(this));
	},
	toggle: function() {
		$(ecw.configuration.content).toggle();
		if($(ecw.configuration.content).visible()) {
			$$("[helpId]").each(function(helpItem) {
				helpItem.addClassName(ecw.configuration.highlighter);
				helpItem.observe("mouseover", this.overListener);
				helpItem.observe("mouseout", this.outListener);
				helpItem.observe("click", this.clickListener);
			}.bind(this));
		}else {
			$$("[helpId]").each(function(helpItem) {
				helpItem.removeClassName(ecw.configuration.highlighter);
				helpItem.stopObserving("mouseover");
				helpItem.stopObserving("mouseout");
			}.bind(this));
		}
	},
	overListener: function() {
		this.addClassName("over");
	},
	outListener: function() {
		this.removeClassName("over");
	},
	clickListener: function(event) {
		var helpId = this.readAttribute("helpId");
		if(helpId && helpId.length > 0) {
			event.stop();
			new Ajax.Updater(
				ecw.configuration.content, 
				ecw.configuration.servicePath + 
					helpId + 
					ecw.configuration.suffix
			);
		}
	}
});